/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to OPNET Translator
 * Author: ewalters
 * Created: 3:43:24 PM, Aug 8, 2007
 * MITRE Corporation
 */
package com.mitre.spmif2opnet;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import com.mitre.spmif2opnet.opnet.network.Network;
import com.mitre.spmif2opnet.spmif.ProjectType;

/**
 * Wrapper class for XML I/O, since we use it in various places
 */
public class XMLInterface {
	/**
	 * Empty constructor 
	 */
	public XMLInterface() { }

	/**
	 * Load from XML into Java format
	 * @param file_name file to load from
	 * @return S-PMIF project type
	 * @throws JAXBException
	 */
	public ProjectType loadXMLFile(String file_name) throws JAXBException {
		JAXBContext jc_in = JAXBContext.newInstance("com.mitre.spmif2opnet.spmif");
		Unmarshaller um = jc_in.createUnmarshaller();
		System.out.println("[INFO] Loading file " + file_name);
		JAXBElement<ProjectType> root = um.unmarshal(new StreamSource(new File(file_name)), 
				ProjectType.class); 

		// Now we have this in Java form!
		return root.getValue();
	}
	
	/**
	 * Export Java out to OPNET XML (using OPNET Network DTD)
	 * @param file_name output file name
	 * @param network the OPNET-based network to output
	 * @throws JAXBException
	 */
	public void exportXMLFile(String file_name, Network network) throws JAXBException {
		JAXBContext jc_out = JAXBContext.newInstance("com.mitre.spmif2opnet.opnet.network");
		Marshaller m = jc_out.createMarshaller();
		System.out.println("[INFO] Exporting file " + file_name);
		m.marshal(network, new File(file_name));
	}
}
